
import { saveUserinfo } from './index.js'
const baseUrl = 'http://admin.api.moyangjia.top'
const getUserInfoAndSaveToken = () => {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: (res1) => {
				console.log('code', res1)
				uni.request({
					url:`${baseUrl}/api/YzUser/login_by_weixin`,
					method: 'POST',
					data: {
					  "code": res1.code,
					},
					success: (res) => {
						console.log('res===login', res)
						if(res.statusCode == 200 && res.data.status == 200) {
							let token = res.data.data.token
							uni.setStorageSync('token', token)
							let userInfo = res.data.data
							uni.setStorageSync('userInfo', userInfo)
							resolve(true)
						} else {
							resolve(false)
						}
					},
					fail: (err) => {
						console.log('err===login', err)
						reject(err)
					}
				})
			}
		})
	})
}
const onGotUserInfo = async (updateData) => {
	let userInfoStorage = uni.getStorageSync('userInfo')
	let data = Object.assign(userInfoStorage, updateData)
	uni.setStorageSync('userInfo', data)
	let res = await saveUserinfo(data)
	console.log('用户昵称和头像落户成功==', res)
	let isLogin = false
	if(res.status == 200) {
		isLogin = true
	}
	return isLogin
}

export {
	getUserInfoAndSaveToken,
	onGotUserInfo
}